package com.ljs.shardingspheredemo.config.algorithm;

import lombok.val;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.math.BigInteger;
import java.util.Collection;

public class MyTablePreciseAlgorithm implements PreciseShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
       String logicTableName = preciseShardingValue.getLogicTableName();
   String userId=preciseShardingValue.getColumnName();
      Long value = preciseShardingValue.getValue();
      //实现  t_user_$->{user_id%3+1}
        //转化为int类型才能取模
        BigInteger bigInteger=BigInteger.valueOf(value);
        BigInteger bb=bigInteger.mod(new BigInteger("3")).add(new BigInteger("1"));//user_id%3+1
        String key=logicTableName+"_"+bb;
        //判断集合中有没有
        if(collection.contains(key)){
            return  key;

        }
        throw new UnsupportedOperationException("请检查配置");


    }
}
